<template>
  <view :style="{background: `#f7f8fc url(${imageBaseUrl}/detailsbj.png) no-repeat top center / 100% 447rpx`}">
    <view class="GuideDetailsTop">
      <view class="GuideDetailsTops">
        <view class="PolicyDocumentLiTitle">{{ policyInfo.policyName }}</view>
        <view class="PolicyDocumentLiTag">
          <view class="PolicyDocumentLiTagli">{{ policyInfo.policyRegion }}</view>
          <view class="PolicyDocumentLiTagli">{{ policyInfo.policyPubdept }}</view>
          <view class="PolicyDocumentLiTagli">{{ getDictLabel(policyInfo.policyType,dictList) }}</view>
        </view>
        <!-- <view class="PolicyDocumentLiType green">申报中 | 30天内截止</view> -->
      </view>
    </view>
    <view class="GuideDetailsBot">
      <view class="GuideDetailsTops">
        <view class="gDetails">
          <view class="gDetailsTop">
            <view class="gDetailsTopTitle">基本信息</view>
            <view class="gDetailsTopList">
              <view class="gDetailsTopListLi">
                <view class="gDetailsTopListLil">匹配分数</view>
                <view class="gDetailsTopListLir">{{ reportContent.score }}分</view>
              </view>
              <view class="gDetailsTopListLi">
                <view class="gDetailsTopListLil">企业名称</view>
                <view class="gDetailsTopListLir">{{ policyInfo.companyName }}</view>
              </view>
              <view class="gDetailsTopListLi">
                <view class="gDetailsTopListLil">社会信用代码</view>
                <view class="gDetailsTopListLir">{{ policyInfo.creditCode }}</view>
              </view>
              <view class="gDetailsTopListLi">
                <view class="gDetailsTopListLil">报告生成时间</view>
                <view class="gDetailsTopListLir">{{ policyInfo.createTime }}</view>
              </view>
              <view class="gDetailsTopListLi">
                <view class="gDetailsTopListLil">报告附件</view>
                <view class="gDetailsTopListLir download-btn" @click="downloadReport()">
                  <image src="/static/images/xiazai.png" mode="aspectFit" class="download-icon"></image>
                  <text>下载报告</text>
                </view>
              </view>
            </view>
          </view>
          <view class="gDetailsTop">
            <view class="gDetailsTopTitle">申报成功率影响因素</view>
            <view class="gDetailsTopListLir list-content">
                <view v-for="(item, index) in reportContent.successFactors" :key="index" class="list-item">
                    {{ item }}
                </view>
            </view>
          </view>
          <view class="gDetailsTop">
            <view class="gDetailsTopTitle">指标详解</view>
            <view class="gDetailsTopListLir list-content">
                <view v-for="(item, index) in reportContent.policyAnalysis" :key="index" class="list-item">
                    {{ item }}
                </view>
            </view>
          </view>
          <view class="gDetailsTop">
            <view class="gDetailsTopTitle">申报条件</view>
            <view class="gDetailsTopListLir list-content">
                <view v-for="(item, index) in reportContent.condition" :key="index" class="list-item">
                    {{ item }}
                </view>
            </view>
          </view>
          <view class="gDetailsTop">
            <view class="gDetailsTopTitle">所需材料</view>
            <view class="gDetailsTopListLir list-content">
                <view v-for="(item, index) in reportContent.material" :key="index" class="list-item">
                    {{ item }}
                </view>
            </view>
          </view>
          <view class="gDetailsTop">
            <view class="gDetailsTopTitle">涨分锦囊</view>
            <view class="gDetailsTopListLir list-content">
                <view v-for="(item, index) in reportContent.suggestion" :key="index" class="list-item">
                  {{ item }}
                </view>
            </view>
          </view>
        </view>
      </view>
      <view class="GuideDetailsBotEnd">
        <gDetails v-if="activeIndex === 0" :policyInfo="policyInfo" />
        <original v-if="activeIndex === 1" :policyInfo="policyInfo" />
      </view>
    </view>
  </view>
</template>

<script>
import { useStore } from 'vuex';
import { DICT_TYPES } from '@/store/modules/dict';
import { getDictLabel } from '@/utils/dict';
export default {
  data() {
    return {
      report: {},
      activeIndex: 0,
      policyInfo: {},
      dictList: [],
      reportContent: {}
    };
  },
  computed: {
	imageBaseUrl() {
		return this.$IMAGE_BASE_URL;
	}
  },
  onLoad(options) {
    if (options.reportData) {
      const reportData = JSON.parse(decodeURIComponent(options.reportData));
      console.log('接收到的报告数据:', reportData);
      this.policyInfo = reportData;
      this.reportContent=reportData.matchPolicy;
    }
    this.getDictData();
  },
  methods: {
			
    getDictLabel,
    downloadReport() {
      // 复制 report.vue 中的下载逻辑
      const url = this.policyInfo.reportAttachmentUrl.replace('http://121.37.118.34:9003', 'https://wvuw.oucuiya.com');
      this.downloadPDF(url);
    },
    downloadPDF(url) {
      // 保持原有的 downloadPDF 方法不变
      uni.showLoading({
        title: '下载中...',
        mask: true
      });
      uni.downloadFile({
        url: url,
        success: (res) => {
          if (res.statusCode === 200) {
            const tempFilePath = res.tempFilePath;
            uni.hideLoading();
            uni.openDocument({
              filePath: tempFilePath,
              fileType: 'pdf',
              success: () => {},
              fail: (err) => {
                uni.showToast({
                  title: '打开文档失败',
                  icon: 'none'
                });
              }
            });
          }
        },
        fail: (err) => {
          uni.hideLoading();
          uni.showToast({
            title: '下载失败',
            icon: 'none'
          });
        }
      });
    },

    async getDictData() {
      const store = useStore();
				// 从Vuex中获取政策类型字典
			this.dictList = await store.dispatch('dict/loadDict', DICT_TYPES.POLICY_TYPE);
    }
  }
}
</script>

<style lang="scss">

.GuideDetailsTop {
  padding: 30rpx 24rpx;

  .GuideDetailsTops {
    padding: 28rpx 33rpx;
    border-radius: 20rpx;
    margin: 0 0 27rpx;
    background: #ffffff;

    .PolicyDocumentLiTitle {
      font-size: 32rpx;
      color: #202020;
    }

    .PolicyDocumentLiTag {
      display: flex;
      justify-content: flex-start;
      padding: 15rpx 0;
      flex-wrap: wrap;
      gap: 15rpx;

      .PolicyDocumentLiTagli {
        padding: 0 13rpx;
        min-height: 40rpx;
        line-height: 40rpx;
        font-size: 24rpx;
        color: #4e5262;
        background: #ecf3fc;
        border-radius: 6rpx;
        display: inline-flex;
        align-items: center;
        max-width: 300rpx;
        overflow: hidden;
        text-overflow: ellipsis;
        white-space: nowrap;
      }
    }

    .PolicyDocumentLiType {
      width: 248rpx;
      height: 40rpx;
      line-height: 40rpx;
      text-align: center;
      font-size: 24rpx;
      color: #ffffff;
      background: url("/static/images/collbj.png") no-repeat center / 248rpx 40rpx;

      &.green {
        width: 248rpx;
        height: 40rpx;
        background: url("/static/images/collbj.png") no-repeat center / 248rpx 40rpx;
      }

      &.blue {
        width: 271rpx;
        height: 40rpx;
        background: url("/static/images/daishenbaobj.png") no-repeat center / 271rpx 40rpx;
      }

      &.gray {
        width: 103rpx;
        height: 40rpx;
        color: #4e5262;
        background: url("/static/images/jieshubj.png") no-repeat center / 103rpx 40rpx;
      }
    }
  }
}
.GuideDetailsBot {
  padding: 0 24rpx;
  .GuideDetailsBotTabs {
    display: flex;
    justify-content: space-between;
    background: url("/static/images/guidebuj1.png") no-repeat bottom center / 100% 94rpx;
  }
}

.gDetails {
  .gDetailsTop {
    margin: 0 0 30rpx;
    padding: 0 40rpx 20rpx;
    background: #ffffff;
    border-radius: 20rpx;
    
    &:first-child {
      padding: 0 40rpx 20rpx;
      margin: 0 0 30rpx;
      
      .gDetailsTopTitle {
        font-size: 32rpx;
        color: #202020;
        padding-left: 40rpx;
        background: url("/static/images/shuangsanjiao.png") no-repeat left center / 22rpx 21rpx;
        height: 110rpx;
        display: flex;
        align-items: center;
      }
      
      .gDetailsTopList {
        padding: 0 0 10rpx;
        .gDetailsTopListLi {
          display: flex;
          line-height: 55rpx;
          justify-content: space-between;
          .gDetailsTopListLil {
            font-size: 28rpx;
            color: #4e5262;
          }
          .gDetailsTopListLir {
            font-size: 28rpx;
          }
        }
      }
    }

    .gDetailsTopTitle {
      font-size: 32rpx;
      color: #202020;
      padding-left: 40rpx;
      background: url("/static/images/shuangsanjiao.png") no-repeat left center / 22rpx 21rpx;
      height: 110rpx;
      display: flex;
      align-items: center;
    }

    .gDetailsTopListLir {
      &.list-content {
        text-align: left;
        padding: 0 20rpx;
        
        .list-item {
          font-size: 28rpx;
          color: #202020;
          padding: 8rpx 0 8rpx 20rpx;
          line-height: 45rpx;
          margin: 0 0 10rpx;
          position: relative;
          
          &::before {
            content: '';
            position: absolute;
            left: 0;
            top: 50%;
            transform: translateY(-50%);
            width: 8rpx;
            height: 8rpx;
            border-radius: 50%;
            background-color: #2f59fe;
          }
          
          &:last-child {
            margin-bottom: 0;
          }
        }
      }
    }

    // 只对指标详解、所需材料保持原有样式
    &:nth-child(3), &:nth-child(5) {
      .gDetailsTopListLir {
        &.list-content {
          .list-item {
            background: #fff7ec;
            padding: 15rpx 22rpx;
            margin: 0 0 20rpx;
            color: #b88645;
            &::before {
              display: none;
            }
          }
        }
      }
    }
  }
}
.gDetailsTopListLir.download-btn {
  background-color: #2f59fe;
  display: flex;
  align-items: center;
  border-radius: 8rpx;
  padding: 0 20rpx;
  height: 56rpx;
  line-height: 56rpx;
  color: #ffffff;
  cursor: pointer;
  transition: all 0.3s ease;

  &:hover {
    background-color: #2f59fe;
  }

  .download-icon {
    width: 32rpx;
    height: 32rpx;
    margin-right: 10rpx;
  }

  text {
    font-size: 28rpx;
  }
}
.gDetailsTopListLir {
  &.list-content {
    text-align: left;
    padding-left: 20rpx;
    
    .list-item {
      padding: 10rpx 0;
      line-height: 1.5;
      color: #202020;
      font-size: 28rpx;
      
      &:first-child {
        padding-top: 0;
      }
      
      &:last-child {
        padding-bottom: 0;
      }
    }
  }
}
.score-unit {
  font-size: 28rpx;
  color: #4e5262;
  margin-left: 4rpx;
}
</style>